Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PMD: LiteralsFirstInComparisons for compareTo* and contentEquals #366

Closed

Conversation

punkratz312
Copy link
Contributor

@punkratz312 punkratz312 requested a review from timtebeek October 14, 2024 13:03
Copy link
Contributor

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some suggestions could not be made:

  • src/main/java/org/openrewrite/staticanalysis/ChainStringBuilderAppendCalls.java
    • lines 35-36

Comment on lines +65 to +78
if (!(m.getSelect() instanceof J.Literal)
&& m.getArguments().get(0) instanceof J.Literal
&& (EQUALS.matches(m)
|| !style.getIgnoreEqualsIgnoreCase()
&& EQUALS_IGNORE_CASE.matches(m)
|| COMPARE_TO.matches(m)
|| COMPARE_TO_IGNORE_CASE.matches(m)
|| CONTENT_EQUALS.matches(m))) {
final Object parent = getCursor().getParentTreeCursor().getValue();
if (parent instanceof J.Binary) {
J.Binary binary = (J.Binary) parent;
if (binary.getOperator() == J.Binary.Type.And && binary.getLeft() instanceof J.Binary) {
J.Binary potentialNullCheck = (J.Binary) binary.getLeft();
if ((isNullLiteral(potentialNullCheck.getLeft()) && matchesSelect(potentialNullCheck.getRight(), m.getSelect())) ||
(isNullLiteral(potentialNullCheck.getRight()) && matchesSelect(potentialNullCheck.getLeft(), m.getSelect()))) {
final J.Binary binary = (J.Binary) parent;
if (binary.getLeft() instanceof J.Binary
&& binary.getOperator() == J.Binary.Type.And) {
final J.Binary potentialNullCheck = (J.Binary) binary.getLeft();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (!(m.getSelect() instanceof J.Literal)
&& m.getArguments().get(0) instanceof J.Literal
&& (EQUALS.matches(m)
|| !style.getIgnoreEqualsIgnoreCase()
&& EQUALS_IGNORE_CASE.matches(m)
|| COMPARE_TO.matches(m)
|| COMPARE_TO_IGNORE_CASE.matches(m)
|| CONTENT_EQUALS.matches(m))) {
final Object parent = getCursor().getParentTreeCursor().getValue();
if (parent instanceof J.Binary) {
J.Binary binary = (J.Binary) parent;
if (binary.getOperator() == J.Binary.Type.And && binary.getLeft() instanceof J.Binary) {
J.Binary potentialNullCheck = (J.Binary) binary.getLeft();
if ((isNullLiteral(potentialNullCheck.getLeft()) && matchesSelect(potentialNullCheck.getRight(), m.getSelect())) ||
(isNullLiteral(potentialNullCheck.getRight()) && matchesSelect(potentialNullCheck.getLeft(), m.getSelect()))) {
final J.Binary binary = (J.Binary) parent;
if (binary.getLeft() instanceof J.Binary
&& binary.getOperator() == J.Binary.Type.And) {
final J.Binary potentialNullCheck = (J.Binary) binary.getLeft();
if (!(m.getSelect() instanceof J.Literal) &&
m.getArguments().get(0) instanceof J.Literal &&
(EQUALS.matches(m) ||
!style.getIgnoreEqualsIgnoreCase() &&
EQUALS_IGNORE_CASE.matches(m) ||
COMPARE_TO.matches(m) ||
COMPARE_TO_IGNORE_CASE.matches(m) ||
CONTENT_EQUALS.matches(m))) {
if (binary.getLeft() instanceof J.Binary &&
binary.getOperator() == J.Binary.Type.And) {
if (isNullLiteral(potentialNullCheck.getLeft()) &&
matchesSelect(potentialNullCheck.getRight(), m.getSelect()) ||
isNullLiteral(potentialNullCheck.getRight()) &&
matchesSelect(potentialNullCheck.getLeft(), m.getSelect())) {

…est.java

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
@punkratz312
Copy link
Contributor Author

So, will the GitHub Action apply the recipes by Moderna, or how is the engine executed? The automation aspect is not yet completed in my team. We have just explored this tool but haven't managed to fully automate the processes yet. However, it has still been helpful.

@punkratz312
Copy link
Contributor Author

#368

@timtebeek
Copy link
Contributor

Please feel free to reuse a PR that's already opened, as opposed to closing and opening a new one. We squash merge, so there's no need to keep a clean history. I'd rather just have a single PR that we can discuss everything on including what we've learned since it was opened, than have that spread across various PRs some of which never get merged.

@timtebeek
Copy link
Contributor

So, will the GitHub Action apply the recipes by Moderna, or how is the engine executed? The automation aspect is not yet completed in my team. We have just explored this tool but haven't managed to fully automate the processes yet. However, it has still been helpful.

We've documented our flow on the Moderne blog: https://www.moderne.ai/blog/stop-breaking-ci-annotate-prs-with-openrewrite-recipe-fixes-as-quality-gate
There's also examples available if you use Maven instead:

I hope that helps you integrate recipe runs into your workflows there. Do note that Moderne would allow you to run these at scale, as opposed to getting a performance penalty on each CI run.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

2 participants